//
//  LZNetworkTool.swift
//  Weibo
//
//  Created by lzing on 16/3/23.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit
import AFNetworking

//MARK: - 使用管理错误信息
enum LZNetworkToolError : Int {
    var netDomin : String {
        return "cn.itcast.error.net"
    }
    var dbDomin : String {
        return "cn.itcast.error.db"
    }
    
    case RepeatRefresh = -1
    case EmptyAccessToken = -250
    
    // 枚举中可以有属性
    var errorDescription : String {
        get {
            switch self {
            case RepeatRefresh : return "嘿哥们,手不要那么快,重复刷新了"
            case EmptyAccessToken : return "access_token 没有值"
            }
        }
    }
    
    // 枚举中可以有方法
    func error() -> NSError {
        return NSError(domain: netDomin, code: self.rawValue, userInfo: ["errorDescription" : self.errorDescription])
    }
}

class LZNetworkTool: NSObject {
    /// 单例
    static let sharedNetworkTool : LZNetworkTool = LZNetworkTool()
    
    private let afnManager : AFHTTPSessionManager = AFHTTPSessionManager(baseURL: LZBaseURL, sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
    
    private override init() {
        super.init()
        afnManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
    }
    
    ///  封装AFN方法,GET请求
    func GET(URLString: String, var parameters: [String : AnyObject]?, completion: (result: [String : AnyObject]?, error: NSError?) -> ()) {
        // 自己拼接accessToken
        guard let accessToken = LZUserAccountViewModel.sharedUserAccountViewModel.userAccount?.access_token else {
            completion(result: nil, error: LZNetworkToolError.EmptyAccessToken.error())
            return
        }
        // 如果没有传入参数
        if parameters == nil {
            parameters = [String : AnyObject]()
        }
        
        parameters!["accessToken"] = accessToken
        
        afnManager.GET(URLString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            let dict = result as? [String : AnyObject]
            completion(result: dict, error: nil)
            }) { (_, error) -> Void in
                completion(result: nil, error: error)
        }
    }
    
    ///  封装AFN方法,POST请求
    func POST(URLString: String, parameters: AnyObject?, completion: (result: [String : AnyObject]?, error: NSError?) -> ()) {
        afnManager.POST(URLString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            let dict = result as? [String : AnyObject]
            completion(result: dict, error: nil)
            }) { (_, error) -> Void in
                completion(result: nil, error: error)
        }
    }
    
    ///  封装AFN方法,POST请求,封装formData
    func POST(URLString: String, parameters: AnyObject?,constructingBodyWithBlock: ((AFMultipartFormData) -> Void)?, completion: (result: [String : AnyObject]?, error: NSError?) -> ()) {
        afnManager.POST(URLString, parameters: parameters, constructingBodyWithBlock: constructingBodyWithBlock, progress: nil, success: { (_, result) -> Void in
            let dict = result as? [String : AnyObject]
            completion(result: dict, error: nil)
            }) { (_, error) -> Void in
                completion(result: nil, error: error)
        }
    }
}
